﻿using System.Text;

namespace AOTToolConsoleApp.Tools
{
    internal class CodeMergerTool : ITool
    {
        private string _srcPath = null;
        private string _dstPath = null;
        private string[] _extensions = null;


        public void Excute(string[] args)
        {
            try
            {
                //args = new string[]
                //{
                //    "-sp",@"D:\Temp\code",
                //    "-dp",@"D:\Temp\code.txt",
                //    "-e",@".cs|.h|.cpp"
                //};

                if (args == null || args.Length < 6)
                {
                    Console.WriteLine("缺少必须参数\n示例: -src D:\\代码根目录 -dst D:\\txt文件存放路径 -e \".h|.cpp|.c\"");
                    return;
                }

                ParseArgs(args[0], args[1]);
                ParseArgs(args[2], args[3]);
                ParseArgs(args[4], args[5]);

                if (string.IsNullOrEmpty(this._srcPath))
                {
                    Console.WriteLine("未指定代码根目录");
                    return;
                }

                if (!Directory.Exists(this._srcPath))
                {
                    Console.WriteLine($"代码根目录\"{this._srcPath}\"不存在");
                    return;
                }

                if (string.IsNullOrEmpty(this._dstPath))
                {
                    Console.WriteLine("未指定txt文件存放路径");
                    return;
                }

                if (this._extensions == null || this._extensions.Length == 0)
                {
                    Console.WriteLine("未指定代码文件扩展名");
                    return;
                }



                StringBuilder sb = new StringBuilder();
                foreach (string ext in this._extensions)
                {
                    string[] fileNames = Directory.GetFiles(_srcPath, $"*{ext}", SearchOption.AllDirectories);
                    foreach (var fileName in fileNames)
                    {
                        if (sb.Length > 0)
                        {
                            sb.AppendLine();
                        }

                        sb.AppendLine(Path.GetFileName(fileName));
                        sb.AppendLine();
                        sb.Append(File.ReadAllText(fileName));
                    }
                }

                string dir = Path.GetDirectoryName(this._dstPath);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                File.WriteAllText(this._dstPath, sb.ToString());
                Console.WriteLine("生成完成");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"生成失败{ex.Message}");
            }


        }

        private void ParseArgs(string paraName, string para)
        {
            switch (paraName.ToLower())
            {
                case "-src":
                   this. _srcPath = para;
                    break;
                case "-dst":
                    this._dstPath = para;
                    break;
                case "-e":
                    this._extensions = para.Split('|', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
                    break;
                default:
                    Console.WriteLine($"无效参数\"{paraName}\"");
                    break;
            }
        }
    }
}
